package com.hwua.view;

import com.hwua.domain.User;
import com.hwua.service.UserService;
import com.hwua.serviceimpl.UserServiceImpl;
import com.hwua.util.InputTools;

import java.util.List;

/**
 * 管理员查看用户信息界面
 * @author admin
 */
public class AdminInformationView extends AbstractView{
    private String isAdmin;
    @Override
    public AbstractView showView() {
        UserService us = new UserServiceImpl();
        List<User> userList = us.showUserInformation();
        System.out.println("==============================<<<用户信息>>>==========================");
        System.out.println("用户id\t\t用户名\t\t密码\t\tVIP等级\t\t是否管理员");
        for (User user : userList) {
           if(user.getIsAdmin()==0){
               isAdmin="否";
           }else{
               isAdmin="是";
           }
            System.out.println(user.getUserId()+"\t\t\t"+user.getUserName()+"\t\t"+user.getUserPwd()+"\t\t"+
                    "\t"+user.getUserLevel()+"\t\t\t"+isAdmin);
        }
        System.out.println();
        System.out.println("--------------------------------------------------------------");
        System.out.println("输入0              退出");
        System.out.println("输入1+用户编号     修改用户VIP等级");
        System.out.println("输入2              返回主界面");
        while (true){
            String choose = InputTools.nextString();
            //使用正则表达式匹配录入的字符串是否符合“1+1”或者“1” 格式
            boolean matches = choose.matches("[0-9]{1}(\\+\\d+)?");
            if(matches){
                //判断键盘录入的字符串长度是否大于1，如果大于1，则截取
                if(choose.length()>1) {
                    //通过String类的split方法分割字符串
                    String[] strings = choose.split("\\+");
                    //  获得键盘录入字符串中 + 号前面的字符并转换成int类型
                    int num1 = Integer.parseInt(String.valueOf(strings[0]));
                    //  键盘录入字符串中 + 号后面的字符并转换成int类型
                    int num2 = Integer.parseInt(String.valueOf(strings[1]));
                    switch (num1){
                        case 1:
                            nextView = new AlterUserLevelView(num2);
                            break;
                        default:
                            System.out.println("超出可选范围，请重新选择：");
                            continue;
                    }
                }else{
                    switch (choose){
                        case "0":
                            nextView=null;
                            break;
                        case "2":
                            nextView = new AdminView();
                            break;
                        default:
                            System.out.println("超出可选范围，请重新选择：");
                            continue;
                    }
                }
                break;
            }else{
                System.out.println("输入有误，请重新输入：");
            }
        }
        return nextView;
    }
}
